package OnlineExam.db;

import org.hibernate.*;
import org.hibernate.cfg.Configuration;
import java.util.*;

/**
 *  A Util class that is used perform DB accesses.
 */

public class DBUtil
{

	/**
	 *  select : Queries a given table on a given column  and its value, returns a list of table objects.
	 */

	public static List select (String table, String className, String paramName, int paramValue) throws DBException
	{
			
		Session session = null;

		try
		{
			SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
			session = sessionFactory.openSession();
	 		Query query = session.createQuery ("from " + table + " in class " + className + " where " + paramName +" = :value"); 
			query.setInteger ("value", paramValue);
			return query.list();
		}
		catch (HibernateException e)
		{
			throw new DBException (e);
		}
		finally
		{
			try { if (session != null) session.flush(); session.close();}
			catch (Exception e) { System.out.println ("Exception.." + e); }
			
		}
	}
	
	/**
	 *  select : selects a given table, returns the table object.
	 */

	public static List select (String table, String className) throws DBException
	{
			
		Session session = null;

		try
		{
			SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
			session = sessionFactory.openSession();
	 		Query query = session.createQuery ("from " + table + " in class " + className); 
			return query.list();
		}
		catch (HibernateException e)
		{
			throw new DBException (e);
		}
		finally
		{
			try { if (session != null) session.flush(); session.close();}
			catch (Exception e) { System.out.println ("Exception.." + e); }
			
		}
	}		

	/**
	 *  select : Queries a given table on a given column  and its value, returns a list of table objects.
	 */

	public static List select (String table, String className, String paramName, String paramValue) throws DBException
	{
			
		Session session = null;

		try
		{
			SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
			session = sessionFactory.openSession();
	 		Query query = session.createQuery ("from " + table + " in class " + className + " where " + paramName +" = :value"); 
			query.setString ("value", paramValue);
			return query.list();
		}
		catch (HibernateException e)
		{
			throw new DBException (e);
		}
		finally
		{
			try { if (session != null) session.flush(); session.close();}
			catch (Exception e) { System.out.println ("Exception.." + e); }
			
		}
	}	


	/**
	 *  select : Queries a given table on a given column  and its value, returns a list of table objects.
	 */

	public static List select (String table, String className, String paramName1, String paramValue1, 
					String paramName2, String paramValue2) throws DBException
	{
			
		Session session = null;

		try
		{
			SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
			session = sessionFactory.openSession();

	 		Query query = session.createQuery ("from " + table + " in class " + className + " where " + paramName1 +" = :value1 and " + paramName2 +"= :value2"); 
			query.setString ("value1", paramValue1);
			query.setString ("value2", paramValue2);

			return query.list();
		}
		catch (HibernateException e)
		{
			throw new DBException (e);
		}
		finally
		{
			try { if (session != null) session.flush(); session.close();}
			catch (Exception e) { System.out.println ("Exception in DBUtil.." + e); }
			
		}
	}	
	
	/**
	 *  select : Queries a given table on a given column  and its value, returns a list of table objects.
	 */

	public static List select (String table, String className, String paramName1, String paramValue1, 
					String paramName2, String paramValue2 , String paramName3, String paramValue3) throws DBException
	{
			
		Session session = null;

		try
		{
			SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
			session = sessionFactory.openSession();

	 		Query query = session.createQuery ("from " + table + " in class " + className + " where " + paramName1 +" = :value1 and " + paramName2 +"= :value2 and " + paramName3 +"= :value3"); 
			query.setString ("value1", paramValue1);
			query.setString ("value2", paramValue2);
			query.setString ("value3", paramValue3);

			return query.list();
		}
		catch (HibernateException e)
		{
			throw new DBException (e);
		}
		finally
		{
			try { if (session != null) session.flush(); session.close();}
			catch (Exception e) { System.out.println ("Exception in DBUtil.." + e); }
			
		}
	}	

	/**
	 *  delete : deletes row(s) in a table whose column and attribute values matches the passed values.
	 */

	public static int delete (String table, String paramName, String paramValue) throws DBException 
	{
			
		Session session = null;
		
		try
		{
			SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
			session = sessionFactory.openSession ();
			
			String queryToDelete = "delete from " + table + " where " + paramName + " = :value";
			Query query = session.createSQLQuery ( queryToDelete );
			query.setString ("value", paramValue);
			return (query.executeUpdate());
			
		}
		catch (HibernateException e)
		{
			throw new DBException (e);
		}
		finally
		{
			try { if (session != null) session.flush(); session.close();}
			catch (Exception e) { System.out.println ("Exception..." + e);}
		}
	}
	
	/**
	 *  delete : deletes row(s) in a table whose column and attribute values matches the passed values.
	 */

	public static int delete (String table, String paramName, int paramValue) throws DBException 
	{
			
		Session session = null;
		
		try
		{
			SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
			session = sessionFactory.openSession ();
			
			String queryToDelete = "delete from " + table + " where " + paramName + " = :value";
			Query query = session.createSQLQuery ( queryToDelete );
			query.setInteger ("value", paramValue);

			return (query.executeUpdate());
			
		}
		catch (HibernateException e)
		{
			throw new DBException (e);
		}
		finally
		{
			try { if (session != null) session.flush(); session.close();}
			catch (Exception e) { System.out.println ("Exception..." + e);}
		}
	}
	
	/**
	 * insert : inserts the given object into the database.
	 */
	
	public static void insert (Object object) throws DBException
	{
		Session session = null;
		try
		{
      		SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
       		session = sessionFactory.openSession();
			
        	session.save(object);

		} 
		catch (HibernateException e) 
		{
				throw new DBException (e);
		}
		finally 
		{
			try { if (session != null ) session.flush(); session.close(); }
			catch (Exception e ) { System.out.println ("Exception...." + e); }
		}
		
	}
	
	/**
	 * update : performs the function of updating the database with the passed object.
	 */
	
	public static void update (Object object) throws DBException
	{
		Session session = null;
		try
		{
      		SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
       		session = sessionFactory.openSession();

        	session.update(object);

		} 
		catch (HibernateException e) 
		{
				throw new DBException (e);
		}
		finally 
		{
			try { if (session != null ) session.flush(); session.close(); }
			catch (Exception e ) { System.out.println ("Exception....." + e); }
		}
	}
}
